package zhcms.tool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MyTool {
	public MyTool() {
	}

	/* 对字符串进行中GBK编码 */
	public String toChinese(String str) {
		String chinese = "";
		try {
			chinese = new String(str.getBytes("ISO8859-1"), "GB2312");
		} catch (Exception ex) {
			System.out.println("中文处理失败！");
		}
		return chinese;
	}

	/* 对字符串进行Unicode编码 */
	public String toUnicode(String str) {
		try {
			if (str == null) {
				return null;
			} else {
				str = new String(str.getBytes("GBK"), "ISO8859_1");
				return str;
			}
		} catch (Exception ex) {
			return "";
		}
	}

	/* 对输入的字符串进行一次编码转换,防止SQL注入 */
	public String StringtoSql(String str) {
		str = nullToString(str, "");
		try {
			str = str.trim().replace('\'', (char) 1);
		} catch (Exception ex) {
			return "";
		}
		return str;
	}

	/* 对字符串进行二次编码转换，防止出库时异常发生 */
	public String SqltoString(String str) {
		str = nullToString(str, "");
		try {
			str = str.replace((char) 1, '\'').trim();
		} catch (Exception ex) {
			return "";
		}
		return str;
	}

	public String nullToString(String str1, String str2) {
		return str1 == null ? str2 : str1;
	}

	/* 空字符串处理方法 */
	public String chStr(String str) {
		return str == null ? "" : str;
	}

	/* 获取当前系统时间 */
	public String Date() {
		Calendar calendar = Calendar.getInstance();
		String strDate = calendar.get(Calendar.YEAR) + "-"
				+ (calendar.get(Calendar.MONTH) + 1) + "-"
				+ calendar.get(Calendar.DATE) + " "
				+ calendar.get(Calendar.HOUR_OF_DAY) + ":"
				+ calendar.get(Calendar.MINUTE) + ":"
				+ calendar.get(Calendar.SECOND);
		return strDate;
	}

	/* 正常输出HTML标记 */
	public String convertStr(String str) {
		if (str == null) {
			str = "";
		} else {
			try {
				str = str.replaceAll("<", "&lt;");
				str = str.replaceAll(">", "&gt;");
				str = str.replaceAll(" ", "&nbsp;");
				str = str.replaceAll("\r\n", "<br>");
			} catch (Exception ex) {
				System.out.println("HTML标记转换失败！");
			}
		}
		return str;
	}

	/* 判断是否为当前时间 */
	public boolean compareNowTime(String date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date d = null;
		try {
			d = format.parse(date);
		} catch (Exception ex) {

		}
		if (System.currentTimeMillis() - 259200000 < d.getTime()) {
			return true;
		}
		return false;
	}
}
